//
//  MoreViewController.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/17.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "MoreViewController.h"
#import "MoreTableViewCell.h"
#import "constant.h"
#import "SDImageCache.h"
#import "MoreCellModel.h"
    
@interface MoreViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) NSArray *listAr;
@property (nonatomic, strong) NSArray *titleAr;
@property (nonatomic, strong) NSArray *iconAr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSString *cacheSize;

@end


@implementation MoreViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"更多";
        self.navigationController.navigationBar.translucent = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTableView];
//    self.view.backgroundColor = [UIColor blackColor];
    
    [self loading];
 
}
- (void)loading{
    _titleAr = @[@"清除缓存",@"给个评价",@"检查新版本",@"商务合作",@"欢迎页",@"关于"];
    _iconAr = @[@"moreClear@2x",@"moreScore@2x",@"moreVersion@2x",@"moreBusiness@2x",@"moreWelcome@2x",@"moreAbout@2x"];
}

- (NSArray *)listAr{
    if (!_listAr) {
        NSMutableArray *temp = [NSMutableArray array];
        for (int i = 0; i < _titleAr.count; i++) {
            
            MoreCellModel *model = [MoreCellModel moreCellModelWithImageName:_iconAr[i] text:_titleAr[i]];
            [temp addObject:model];
        }
        
        _listAr = temp;
        
    }
    return _listAr;
}

- (void)addTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, JWScreenHeight - 64) style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor clearColor];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.separatorColor = [UIColor grayColor];
    
    [self.view addSubview:_tableView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listAr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MoreTableViewCell *cell = [MoreTableViewCell moreTableViewCellWith:tableView];
    cell.cellModel = self.listAr[indexPath.row];
        
    if (indexPath.row == 0) {
        UILabel *detail = [[UILabel alloc] initWithFrame:CGRectMake(JWScreenWidth - 70 - 5, 0, 70, 20)];
        
        SDImageCache *cache = [SDImageCache sharedImageCache];
        CGFloat size = [cache getSize];
        
        detail.text = [NSString stringWithFormat:@"%.1fM",size / 1000 / 1000];//字节转化，苹果机为1000单位
        _cacheSize = detail.text;
        
        detail.font = [UIFont systemFontOfSize:15];
        detail.textAlignment = NSTextAlignmentRight;
        detail.textColor = [UIColor whiteColor];
        cell.accessoryView = detail;
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"发现%@的垃圾，清理一下吧！",self.cacheSize] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"清理", nil];
        alert.tag = 1;
        alert.delegate = self;
        [alert show];
    }
    if (indexPath.row == 2) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本更新" message:@"检查存在新版本3.2.0.69954    系统词频: 20131225  组词数据: 20110307  辅助码  : 20101217   编译时间: May 29 2015 19:47:33    平台版本: OS X 10.10.4(14E46)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
        alert.tag = 2;
        alert.delegate = self;
        [alert show];
    }
    
  
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 1 && buttonIndex == 1) {
        [self clearIamgeCache];
    }
}

- (void)clearIamgeCache{
    
    [[SDImageCache sharedImageCache] clearDisk];
    [self.tableView reloadData];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"完成" message:@"垃圾已经清理干净！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
